let choice = document.getElementsByName("cho");
let tow = document.getElementById("tow");
let thiree = document.getElementById("thiree");
let qus = document.getElementById("choice");
var show = document.getElementById('show');
var refurbish = document.getElementById('refurbish');
var inputs = undefined;
var result = undefined;
refurbish.onclick = function () {
    location.reload();
}

/*
*求两个三维向量夹角的余弦值
*/
function EE(x1,y1,z1,x2,y2,z2){
    return (x1*x2+y1*y2+z1*z2)/(sqrt(x1*x1+y1*y1+z1*z1)*sqrt(x2*x2+y2*y2+z2*z2));
}

function choiceDim(e) {
    qus.style.display = "none";
    if (this.value == "二维向量") {
        tow.style.display = "block";
        drow2DAxes(context);
        inputs = tow.getElementsByTagName("input");
        result = tow.getElementsByClassName("result")[0];
        for (let i = 0; i < inputs.length; i++) {
            inputs[i].onmouseover = hint;
        }
        show.onclick = buTowOnclick;
    } else if (this.value == "三维向量") {
        thiree.style.display = "block";
        drow3DAxes(context);
        inputs = thiree.getElementsByTagName("input");
        result = thiree.getElementsByClassName("result")[0];
        for (let i = 0; i < inputs.length; i++) {
            inputs[i].onmouseover = hint;
        }
        show.onclick = buThireeOnclick;
    }
}
choice[0].onclick = choiceDim;
choice[1].onclick = choiceDim;

async function buTowOnclick() {
    let a1 = inputs[0].value;
    let a2 = inputs[1].value;
    let b1 = inputs[2].value;
    let b2 = inputs[3].value;
    let a11 = 0;
    let a22 = 0;
    let b11 = 0;
    let b22 = 0;
    try {
        a11 = eval(a1);
        a22 = eval(a2);
        b11 = eval(b1);
        b22 = eval(b2);
    } catch (error) {
        alert("输入格式不正确，请重新输入！");
        return;
    }
    let re = a11 * b22 - b11 * a22;
    context.lineWidth = 3;
    context.font = "bold italic 15pt Times new roman";
    context.fillStyle = "black";
    //uft-8
    //alpha => u03B1
    //beta => u03B2
    await drowVector3(context, a11, a22, 0, 0, "red");
    context.fillText('\u03B1', a11 * c1, -a22 * c1);
    await drowVector3(context, b11, b22, 0, 0, "green");
    context.fillText('\u03B2', b11 * c1, -b22 * c1);
    if (re == 0) {
        await drowVector3(context, b11, b22, a11, a22, "blue");
        result.style.color = "gray";
        result.innerHTML = "线性相关";
    } else {
        result.style.color = "green";
        result.innerHTML = "线性无关";
    }
}

async function buThireeOnclick() {
    let a1 = inputs[0].value;
    let a2 = inputs[1].value;
    let a3 = inputs[2].value;
    let b1 = inputs[3].value;
    let b2 = inputs[4].value;
    let b3 = inputs[5].value;
    let r1 = inputs[6].value;
    let r2 = inputs[7].value;
    let r3 = inputs[8].value;
    let a11 = 0;
    let a22 = 0;
    let a33 = 0;
    let b11 = 0;
    let b22 = 0;
    let b33 = 0;
    let r11 = 0;
    let r22 = 0;
    let r33 = 0;
    try {
        a11 = eval(a1);
        a22 = eval(a2);
        a33 = eval(a3);
        b11 = eval(b1);
        b22 = eval(b2);
        b33 = eval(b3);
        r11 = eval(r1);
        r22 = eval(r2);
        r33 = eval(r3);
    } catch (error) {
        alert("输入格式不正确，请重新输入！");
        return;
    }
    context.lineWidth = 3;
    let re = threeDet([
        [a11, a22, a33],
        [b11, b22, b33],
        [r11, r22, r33]
    ]);

    await drow3DVector(context, a11, a22, a33, false, "red");
    addFont_3D(context, "\u03B1", a11, a22, a33, "black", "bold italic 15pt Times new roman");
    await drow3DVector(context, b11, b22, b33, false, "green");
    addFont_3D(context, "\u03B2", b11, b22, b33, "black", "bold italic 15pt Times new roman");
    await drow3DVector(context, r11, r22, r33, false, "blue");
    addFont_3D(context, "\u03B3", r11, r22, r33, "black", "bold italic 15pt Times new roman");
    await Sleep(2000);
    if (re != 0) {
        drow3Dparallelogram(context, a11, a22, a33, b11, b22, b33);
        await Sleep(2000);
        drowCubeXYZ(context, a11, a22, a33, b11, b22, b33, r11, r22, r33, true, false);
        result.style.color = "green";
        result.innerHTML = "线性无关";
    } else {

        
        let x1 = EE(a11,a22,a33,b11,b22,b33);
        let x2 = EE(a11,a22,a33,r11,r22,r33);
        let x3 = EE(b11,b22,b33,r11,r22,r33);
        let x = [x1, x2, x3];

        /*
        *冒泡排序
        */
        for (let k = 1; k < x.length; k++) {
            for (let i = 0; i < x.length-k; i++) {
                if (x[i] > x[i + 1]) {
                    let j = x[i];
                    x[i] = x[i + 1];
                    x[i + 1] = j;
                }
            }
        }
        if(x[0] == x1){
            drow3Dparallelogram(context, a11, a22, a33, b11, b22, b33);
        }else if(x[0] == x2){
            drow3Dparallelogram(context, a11, a22, a33, r11, r22, r33);
        }else{
            drow3Dparallelogram(context, r11, r22, r33, b11, b22, b33);
        }
        result.style.color = "purple";
        result.innerHTML = "线性相关";
    }

}